const Provinces = require("../models/provincesModel");
const Cities = require("../models/citiesModel");
const Areas = require("../models/areasModel");
const Streets = require("../models/streetsModel");
const Villages = require("../models/villagesModel");

//获取所有省
exports.getProvinces = async (req, res) => {
  try {
    const ProvincesList = await Provinces.find();
    res.status(200).json({
      status: "success",
      data: ProvincesList,
    });
  } catch (err) {
    console.log("获取所有省失败", err);
    res.status(404).json({
      Error: err,
    });
  }
};

//获取某省所有市
exports.getCities = async (req, res) => {
  let code = req.query.code;
  console.log("code---", code);
  try {
    const CitiesList = await Cities.find({ provinceCode: code });
    res.status(200).json({
      status: "success",
      data: CitiesList,
    });
  } catch (err) {
    console.log("获取所有省失败", err);
    res.status(404).json({
      Error: err,
    });
  }
};

//获取某市所有县
exports.getAreas = async (req, res) => {
  let code = req.query.code;
  try {
    const AreasList = await Areas.find({ cityCode: code });
    res.status(200).json({
      status: "success",
      data: AreasList,
    });
  } catch (err) {
    console.log("获取所有省失败", err);
    res.status(404).json({
      Error: err,
    });
  }
};

//获取某县所有镇
exports.getStreets = async (req, res) => {
  let code = req.query.code;
  try {
    const StreetsList = await Streets.find({ areaCode: code });
    res.status(200).json({
      status: "success",
      data: StreetsList,
    });
  } catch (err) {
    console.log("获取所有省失败", err);
    res.status(404).json({
      Error: err,
    });
  }
};

//获取某镇所有村
exports.getVillages = async (req, res) => {
  let code = req.query.code;
  try {
    const VillagesList = await Villages.find({ streetCode: code });
    res.status(200).json({
      status: "success",
      data: VillagesList,
    });
  } catch (err) {
    console.log("获取所有省失败", err);
    res.status(404).json({
      Error: err,
    });
  }
};
